package com.drugStorage.spring.ServiceImpl;

import java.util.List;

import com.drugStorage.domain.OutDrugList;
import com.drugStorage.domain.OutStorageBill;
import com.drugStorage.domain.SumDrugStorageList;
import com.drugStorage.hibernate.dao.OutStorageBillDao;
import com.drugStorage.hibernate.dao.SumDrugStorageListDao;
import com.drugStorage.spring.service.OutStorageService;

public class OutStorageServiceImpl implements OutStorageService {

	private SumDrugStorageListDao sumDrugStorageListDao;
	private OutStorageBillDao outStorageBillDao;

	// private DetailDrugStorageListDao detailDrugStorageListDao;

	public SumDrugStorageListDao getSumDrugStorageListDao() {
		return sumDrugStorageListDao;
	}

	public void setSumDrugStorageListDao(
			SumDrugStorageListDao sumDrugStorageListDao) {
		this.sumDrugStorageListDao = sumDrugStorageListDao;
	}

	public OutStorageBillDao getOutStorageBillDao() {
		return outStorageBillDao;
	}

	public void setOutStorageBillDao(OutStorageBillDao outStorageBillDao) {
		this.outStorageBillDao = outStorageBillDao;
	}

	public boolean AddOutDrugListToOutStorageBill(OutDrugList outDrugList,
			int id) {

		OutStorageBill outStorageBill = outStorageBillDao.getOutStorageBill(id);
		outDrugList.setOutStorageBill(outStorageBill);
		// 获得出库单的信息
		//DrugScience drug = outDrugList.getDrug();
		int DrugID = outDrugList.getDrug().getId();
        int amount = outDrugList.getAmount();
        if(sumDrugStorageListDao.getSumDrugStorageListByDrugID(DrugID)==null)
        {
        	return false;
        }else if(sumDrugStorageListDao.getSumDrugStorageListByDrugID(DrugID).getAmount()<amount){
        	
        	return false;
        }
        else{
        	
        SumDrugStorageList sumDrugStorageList =	sumDrugStorageListDao.getSumDrugStorageListByDrugID(DrugID);
        sumDrugStorageList.setAmount(sumDrugStorageList.getAmount()-amount);
        sumDrugStorageListDao.updateSumDrugStorageList(sumDrugStorageList);
        outStorageBillDao.SaveOutDrugListById(outDrugList);
        	return true;
        }
        		
		

	}

	public void DelOutDrugList(OutDrugList outDrugList) {
		// TODO Auto-generated method stub

	}

	public OutStorageBill getInStorageBill(int id) {
		
		return outStorageBillDao.getOutStorageBill(id);
	}

	public List<OutDrugList> getOutDrugListByOutstorageBillId(int id) {
		
		return outStorageBillDao.getOutDrugListByOutStorageBillid(id);
	}

	public List<OutStorageBill> getOutStorageBillList() {
	
		return outStorageBillDao.getOutStorageBillList();
	}

	public void saveOutStorageBill(OutStorageBill outStorageBill) {
		outStorageBillDao.saveOutStorageBill(outStorageBill);

	}

}
